/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.chatapp.Databaseacces;

import org.xml.sax.*;             
import org.xml.sax.helpers.*;   
import java.io.*;                 
import java.util.*; 
import java.text.SimpleDateFormat;
import java.text.ParseException;

/**
 *
 * @author maggiesd
 */
public class UserHandler extends org.xml.sax.helpers.DefaultHandler{
    
    private User user;
    private StringBuffer accumulator;
        
    
    public UserHandler () {
	user = new User();
    }

    public UserHandler (User usr) {
	user = usr;
    }
    
    // Called at the beginning
    public void startDocument() {
	accumulator = new StringBuffer();
    }

    // When the parser encounters plain text (not XML elements), it calls this method, which accumulates 
    // the text in a string buffer. Note that this method may be called in multiple times.
    public void characters(char[] buffer, int start, int length) {
	accumulator.append(buffer, start, length);
    }

    // At the beginning of each new element, erase any accumulated text
    public void startElement(String namespaceURL, String localName, String qname, Attributes attributes) {
	accumulator.setLength(0);
    }

    // Take special action when reach the end of selected elements. 
    // In this case the accumulated text is stored in the photo object
    public void endElement(String namespaceURL, String localName, String qname)
    {
	if(localName.equals("nickname")){
            String name = accumulator.toString().trim();
            user.setName(name);
        }

        else if(localName.equals("birthdate")){
           String country = accumulator.toString().trim();
            user.setBirth(country);

            }        
        else if(localName.equals("country")){
            String country = accumulator.toString().trim();
            user.setCountry(country);
        }
        else if(localName.equals("email")){
            String mail = accumulator.toString().trim();
            user.setMail(mail);
        }
        else if (localName.equals("userTags")) {        
            String tags = accumulator.toString().trim();
            user.setTags(tags);
	}
    }

    // Called at the end
    public void endDocument() {
    }

    public void warning(SAXParseException exception) {
	System.err.println("WARNING: line " + exception.getLineNumber() + ": "+
			   exception.getMessage());
    }

    public void error(SAXParseException exception) {
	System.err.println("ERROR: line " + exception.getLineNumber() + ": " +
			   exception.getMessage());
    }

    public void fatalError(SAXParseException exception) throws SAXException {
	System.err.println("FATAL: line " + exception.getLineNumber() + ": " +
			   exception.getMessage());
	throw(exception);
    }   
}
